package com.fornknow.saxbuilder;

import com.fornknow.dao.UserDAO;
import com.fornknow.model.User;
import com.fornknow.service.UserService;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassPathXmlApplicationContext  implements  BeanFactory{
    private Map<String,Object>beans =new HashMap<String, Object>();
   //在构造器当中要解析beans.xml文件
    public ClassPathXmlApplicationContext () throws Exception {
        //创建jdm中提供号的SAXBuilder对象
        SAXBuilder saxBuilder = new SAXBuilder();
        //读取beans.xml文件,并构造文档对象
        Document doc = saxBuilder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
        //获取到根元素
        Element root = doc.getRootElement();
        //获取到子元素<bean>
        List list = root.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element element = (Element) list.get(i);
            //获取到bean中的属性值
            String id = element.getAttributeValue("id");
            String clazz = element.getAttributeValue("class");
            //需要将clazz转换为Object对象
            Object o = Class.forName(clazz).newInstance();
            System.out.println(id);
            System.out.println(clazz);
            System.out.println(o);
            //将获取到的id和对象保存到Map容器中
            beans.put(id, o);


            element.getChildren("property");
            for (Element propertyElement : (List<Element>) element.getChildren("property")) {
                String name = propertyElement.getAttributeValue("name");//userDAO
                String bean = propertyElement.getAttributeValue("bean");//u

                //获取Map容器中的UserDAOImpl
                Object beanObject = beans.get(bean);//UserDImpl对象
                //调用Public void =setUserDAO   userDAO方法
                String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                //beanObject.getClass().getInterfaces()[0]形参-------UserDAO
                Method method = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
                //调用set方法 :o:UserService  beanObject就是UserDAO接口的实现类对象（实际参数）
                method.invoke(o,beanObject);

            }
        }
    }

    public Object getBean(String id) {
        return beans.get(id);
    }

    public static void main(String[] args) throws Exception{
//        BeanFactory beanFactory=new ClassPathXmlApplicationContext();
//        UserService service=new UserService();
//        UserDAO userDAO  = (UserDAO) beanFactory.getBean("u");
//        service.setUserDAO(userDAO);
//        service.add(new User());

        BeanFactory factory=new ClassPathXmlApplicationContext();
        UserService userService = (UserService) factory.getBean("userService");
        userService.add(new User());
    }
}
